#!/bin/bash
[ $# -eq 0 ] && echo "Usage:$0 <(base)num1[(tobase)]> <(n)num2[(tobase)]> ...(tobase default 2)" && exit;
a1="0123456789ABCDEF"

while [ $# -gt 0 ];do
        read base num tobase<<<"${1//[()]/ }"
        if [ -n "${base//[0-9]}" -o \( $base -lt 3 -o $base -gt 16 \) ]; then
                echo "${1}: $base is not a valid base!!"
                shift
                continue
        fi
        [ -z "$tobase" ] && tobase=2
        if [ -n "${tobase//[0-9]}" -o \( $tobase -lt 1 -o $tobase -gt 16 \) ]; then
                echo "${1}: $tobase is not a valid base!!"
                shift
                continue
        fi
       
        num="$(echo $num | sed 'y/abcdef/ABCDEF/')"
        svalid=${a1:0:$base}
        if [ -n "${num//[$svalid]}" ];then
                echo "${1}: $num is not a valid base $base number!!"
                shift
                continue
        fi
        array=($(echo $num | sed 's/./& /g;s/A/10/g;s/B/11/g;s/C/12/g;s/D/13/g;s/E/14/g;s/F/15/g'))
        f=1
        n10=0
        for((i=${#array[*]}-1;i>=0;i--));do
                ((n10+=${array[$i]}*f))
                ((f *= $base))
        done
        echo -n "${1}: (10)$n10 ==>($tobase)"
        n2=""
        while [ $n10 -gt 0 ];do
                n2="${a1:$((n10%tobase)):1}$n2"
                ((n10/=tobase))
        done
        echo $n2
        shift
done
echo "Done!!"
